﻿using Gp.Scripts.Character;
using Gp.Scripts.GUI;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.Core {
    [AutoLoad()]
    public class InventorySystem : BaseGameSystem {
        private InventoryModel Model => this.GetModel<InventoryModel>();

        public PlayerInventory PlayerInventory => Model.PlayerInventory;
        
        
        
        
        protected override void OnInit() {
            var model = new InventoryModel();
            MainArchitecture.Interface.RegisterModel(model);
        }

        public override void OnStart()
        {
            // var model = this.Model;
            // var player = new GameObject();
            // model.PlayerInventory = player.AddComponent<PlayerInventory>();
            // model.PlayerInventory.name = PlayerConst.InventoryName;
            // model.PlayerInventory.CharacterId = PlayerConst.PlayerName;
            //
            // // 获取物品
            // model.PlayerInventory.FillCharacterInventoryData(model.GetData(PlayerInventory));
        }

        
        
        /// <summary>
        ///  移除内存中的物品
        /// </summary>
        public void RemoveFromInventory(string itemID, BaseInventory inventory, int amount = 1) {
        }
        
        
        public void RemoveFromInventory(string itemID, int amount = 1) {
            RemoveFromInventory(itemID, PlayerInventory, amount);
        }
        
        


        public void HandleCharacterBuild(NpcBaseCharacter character) {
            // if (character == null) return;
            // CreateInventory(character.GetUniqueName(), character.Info);
        }
        
        

        /// <summary>
        /// 获得某个角色的库存，如果角色合法，一定会返回结果
        /// </summary>
        /// <returns></returns>
        public CharacterInventory GetInventory(string inventoryName, string characterName) {
            return null;
        }


        // /// <summary>
        // /// 打开库存UI
        // /// </summary>
        // public void OpenCharacterInventoryUI(string inventoryName, string characterName) {
        //     if (!Validate(inventoryName, characterName)) return;
        //     InventoryUIGlobal.Get<InputManager>().OpenInventory(GetInventory(inventoryName, characterName));
        // }

        
        public void TryOpenInventoryUI(BaseInventory inventory) {
            // if (InventoryUIInputManager.Instance.InventoryIsOpen) {
            //     return;
            // }
            //
            // InventoryUIInputManager.Instance.OpenInventory(inventory);
        }

        
        public void TryOpenLootUI(LootInventory inventory) {
        }


        public void SendToInventory(BaseInventory source, BaseInventory destination, int slotId) {
            if (source == null || destination == null) {
                return;
            }
            
            if (source == destination) {
                return;
            }
        }

        
        public void SendToInventory(BaseInventory source, int slotId) {
            SendToInventory(source, PlayerInventory, slotId);
        }


        private bool Validate(string inventoryName, string characterName) {
            if (Model.CheckInventory(inventoryName, characterName))
                return true;
            Debug.LogError($"Inventory [{inventoryName}] of target [{characterName}] not found");
            return false;
        }
    }
}